package com.ruoyi.web.controller.pc.uc;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.page.PageUtils;
import com.ruoyi.common.validator.group.AddGroup;
import com.ruoyi.common.validator.group.UpdateGroup;
import com.ruoyi.web.domain.UcQuoProject;
import com.ruoyi.web.domain.UcQuoTable;
import com.ruoyi.web.domain.vo.QueryVo;
import com.ruoyi.web.service.IUcQuoTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


@RestController
@RequestMapping("/userConfig/ucQuoTable")
public class UcQuoTableController extends BaseController {


    @Autowired
    private IUcQuoTableService ucQuoTableService;



    @PreAuthorize("@ss.hasPermi('userConfig:ucQuoTable:list')")
    @GetMapping("/list")
    public TableDataInfo list(QueryVo queryVo) {
        PageUtils page = ucQuoTableService.listByPage(queryVo);
        return getDataTable(page);
    }

    @PreAuthorize("@ss.hasPermi('userConfig:ucQuoTable:add')")
    @Log(title = "申请项目配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated(AddGroup.class) @RequestBody UcQuoTable ucQuoTable){
        return toAjax(ucQuoTableService.insert(ucQuoTable));
    }

    @PreAuthorize("@ss.hasPermi('userConfig:ucQuoTable:edit')")
    @Log(title = "申请项目配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Validated(UpdateGroup.class) @RequestBody UcQuoTable ucQuoTable){
        return toAjax(ucQuoTableService.update(ucQuoTable));
    }


    @GetMapping("/getTable/{id}")
    public AjaxResult getTable(@PathVariable("id") Long id){

        UcQuoTable ucQuoTable = ucQuoTableService.getBaseMapper().selectById(id);

        List<UcQuoProject> ucQuoProjects = ucQuoTableService.getUcQuoProject(id);
        ucQuoTable.setUcQuoProjects(ucQuoProjects);

        return success(ucQuoTable);

    }

    @PostMapping("/setIsUsed")
    public AjaxResult setIsUsed(@RequestBody UcQuoTable ucQuoTable){
        ucQuoTableService.setIsUsed(ucQuoTable);
        return success();
    }



    @GetMapping("/getUsedTable")
    public AjaxResult getUsedTable(){

        QueryWrapper<UcQuoTable> qwQueryWrapper = new QueryWrapper<>();
        qwQueryWrapper.eq("is_used",1);
        UcQuoTable ucQuoTable = ucQuoTableService.getBaseMapper().selectOne(qwQueryWrapper);

        List<UcQuoProject> ucQuoProjects = ucQuoTableService.getUcQuoProject(ucQuoTable.getId());
        ucQuoTable.setUcQuoProjects(ucQuoProjects);

        return success(ucQuoTable);

    }
}

